from args import *
import requests
import math
from datetime import datetime
def cal_days(start_date, end_date):
    start_date = (datetime.strptime(start_date, "%Y-%m-%d"))
    end_date = (datetime.strptime(end_date, "%Y-%m-%d"))
    return (end_date - start_date).days + 1


# 调整使得实际分数在[0.5, 0.9]
# 0.3 对应实际的分数为 0.7
# 0.1 对应实际的分数为 0.5
def cast_scores(sim):
    if sim < default_input_min_score:
        return default_actual_min_score
    if sim > default_input_max_score:
        return default_actual_max_score
    if sim >= default_input_min_score and sim < default_display_normal_days:
        return (default_actual_min_score + ( (sim - default_input_min_score) / (default_display_normal_days - default_input_min_score))
                * (default_normal_days - default_actual_min_score))

    # if sim >= default_display_normal_days and sim <= default_input_max_score:
    return (default_normal_days + ((sim - default_display_normal_days) / (default_input_max_score - default_display_normal_days))
                * (default_actual_max_score - default_normal_days))


def change_day_cast_sim(start_date, end_date, sim):
    interval = cal_days(start_date, end_date)
    sim = cast_scores(sim)
    print(sim)
    if interval <= 7:
        pass
    elif interval <= 30:
        sim_days = default_normal_days + (default_more_than_30_days - default_normal_days) \
                    * ((interval - 7) * 1.0 / 23)
        sim = sim * (sim_days) / default_normal_days
    else:
        sim = default_more_than_30_days
    if sim < 0.1:
        return 0.1
    elif sim > 0.9:
        return 0.9
    return sim
def get_data(start_date, end_date, sim,area, type, base_url=base_request_url):
    # if start_date != end_date:
    sim = change_day_cast_sim(start_date, end_date, sim)
    base_url = f'{multi_days_request_url}&aggWeight={sim}'
    print(f"area: {area}, type: {type}")

    url = f'{base_url}&startDate={start_date}&endDate={end_date}&area={area}&type={type}'
    data = requests.get(url).json()['data']
    data = wrap_data(data)
    for d in data:
        d[0] = d[0][:min(len(d[0]), max_input_len)]
    return data


def wrap_(d):
    if not d:
        return ""
    return d


def wrap(d):
    title = d.get("title", "")
    content = d.get("content", "")
    title = wrap_(title)
    content = wrap_(content)
    return title + " " + content


def wrap_data(data: list[dict]):
    ret = []
    for d in data:
        cluster = d['cluster']

        title_content = [wrap(c) for c in cluster]
        ids = [c['id'] for c in cluster]
        if title_content.count(" ") == len(title_content):
            continue
        title_content = '|'.join(title_content)
        simNum = d['simHotNum']
        ret.append([title_content, simNum, ids])
    return ret
